<template>
  <div class="main">
    <div class="main-content">
      <div class="main-login">
        <div class="main-login-left">
          <img src="../assets/img/endlogin.png" alt />
        </div>
        <div class="main-login-right">
          <div class="main-login-right-title">
            <img src="../assets/img/logo.png" alt />
            <span>值班系统后台管理</span>
          </div>
          <el-form
            ref="resetLoginRef"
            :rules="loginFormRules"
            :model="loginForm"
            label-width="100px"
          >
            <el-form-item prop="user_name" label="用户名：">
              <el-input v-model="loginForm.user_name" placeholder="输入用户名"></el-input>
            </el-form-item>
            <el-form-item label="密码：" prop="password">
              <el-input
                type="password"
                v-model="loginForm.password"
                show-password
                placeholder="输入密码"
              ></el-input>
            </el-form-item>
            <el-form-item style="text-align:center">
              <el-button @click="loginSubmit" type="primary">登录</el-button>
              <el-button @click="resetForm('resetLoginRef')" style="margin-left:40px;">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 登录表单数据
      loginForm: {
        user_name: "Admin112233",
        password: "Admin112233"
      },
      // 表单验证规则
      loginFormRules: {
        user_name: [
          {
            required: true,
            message: "用户名",
            trigger: "blur"
          },
          {
            min: 6,
            max: 16,
            message: "长度在 6 到 16 个字符",
            trigger: "blur"
          }
        ],
        password: [
          {
            required: true,
            message: "请输入密码",
            trigger: "blur"
          },
          {
            min: 6,
            max: 16,
            message: "长度在 6 到 16 个字符",
            trigger: "blur"
          }
        ]
      }
    };
  },
  methods: {
    // 点击登录
    loginSubmit() {
      this.$refs.resetLoginRef.validate(async valid => {
        // 判断验证是否通过
        if (!valid) return;
        //  发起请求
        this.$http
          .post("OAuth/login", this.loginForm)
          .then(res => {
            console.log(res);
            if (res.data.code !== 201) return this.$message.error(res.data.msg);
            console.log(res.data);
            //1.将登陆之后的token，保存到客户端的sessionStorage中
            window.sessionStorage.setItem(
              "token",
              "Bearer " + res.data.data.token
            );
            window.sessionStorage.setItem("user_ID", res.data.data.user_id);
            this.$message.success(res.data.msg);
            //2.通过编程式导航跳转到主页
            this.$router.push("/admin");
          })
          .catch(err => {
            this.$message({
              dangerouslyUseHTMLString: true,
              showClose: true,
              message: err.response.data.data.join("<br>"),
              type: "error"
            });
          });
      });
    },
    // 重置
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
};
</script>

<style lang='less' scoped>
.mian {
  width: 100%;
  height: 100%;
  min-height: 1080px;
  position: relative;
}

.main-content {
  width: 100%;
  height: 937px;

  background: url("../assets/img/bg1.png") no-repeat;
  background-size: cover;
  background-position: center;
}

.main-login {
  display: flex;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  width: 948px;
  height: 438px;
  background: rgba(255, 255, 255, 1);
  box-shadow: 0px 11px 16px 4px rgba(0, 0, 0, 0.25);
  border-radius: 8px;

  .main-login-left {
    display: flex;
    justify-content: center;
    align-items: center;
    width: 522px;
    height: 438px;
    background: rgba(255, 255, 255, 1);
    box-shadow: 0px 11px 16px 4px rgba(0, 0, 0, 0.25);
    border-radius: 8px;
  }

  .main-login-right {
    flex-wrap: wrap;
    display: flex;
    width: 428px;
    height: 438px;
    justify-content: center;
    background: rgba(255, 255, 255, 1);
    box-shadow: 0px 11px 16px 4px rgba(0, 0, 0, 0.25);
    border-radius: 0px 8px 8px 0px;
    box-sizing: border-box;
    padding: 40px;

    .main-login-right-title {
      display: flex;
      margin: 0;
      justify-content: center;
      width: 80%;
      height: 20%;

      img {
        height: 54px;
        width: 54px;
        margin-right: 20px;
        vertical-align: middle;
      }

      span {
        line-height: 50px;
      }
    }

    .el-form {
      .el-form-item {
        margin-bottom: 40px;
      }
    }
  }
}
</style>